//
//  WanChengViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/8/12.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "WanChengViewController.h"

@interface WanChengViewController ()

@end

@implementation WanChengViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.datasDic removeAllObjects];
    

    [self mnporeg];
    [self getHuoQuDingDan];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
       self.dic = [[NSMutableDictionary alloc]initWithCapacity:0];
    [self.tableVIew1 registerNib:[UINib nibWithNibName:@"QuanBuTableViewCell" bundle:nil] forCellReuseIdentifier:@"quanbu"];
     self.datasDic = [[NSMutableArray alloc]initWithCapacity:0];
    page =1;
//    [self getHuoQuDingDan];
    // 下拉刷新
    [self.tableVIew1 addPullToRefreshWithActionHandler:^
     {
         
         [self performSelector:@selector(xialaSuaXin) withObject:self afterDelay:1.5];
         
     }];
    //上推加载更多
    [self.tableVIew1 addInfiniteScrollingWithActionHandler:^{
        
        [self performSelector:@selector(JiaZaiGengDuo) withObject:self afterDelay:1];
    }];
    
}
- (void)xialaSuaXin{
    page =1;
    [self.datasDic removeAllObjects];
    [self getHuoQuDingDan];
    
}

- (void)JiaZaiGengDuo{
    
    page ++;
    if (page<=self.indePage)
    {
        
        [self getHuoQuDingDan];
    }
    else{
        [self.tableVIew1.infiniteScrollingView stopAnimating];
        
        [self.tableVIew1.pullToRefreshView stopAnimating];
        
        hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        hud.mode=MBProgressHUDModeText;
        
        hud.labelText=@"没有更多数据了";
        
        hud.removeFromSuperViewOnHide=YES;
        
        [hud hide:YES afterDelay:1.5];
        
    }
}

- (void)mnporeg{
    
    hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"数据加载中...";
}
- (void)getHuoQuDingDan{
  
 
    
   
    
    long  userId =[[[NSUserDefaults standardUserDefaults]objectForKey:@"ID"] longValue];
    [self.dic setValue:@(userId) forKey:@"userId"];
    [self.dic setValue:@(page) forKey:@"page"];
    [self.dic setValue:@(10) forKey:@"rows"];
    [self.dic setValue:URL_MEIPINGJIADE forKey:@"url"];
    
    [QingQiuShuJu HuoQuWoDeDingDan:self.dic Block:^(NSDictionary *dic, BOOL isOK) {
        
//        NSLog(@"---%@",dic);
        if (isOK) {
            
            [self.datasDic addObjectsFromArray:[dic valueForKey:@"data"]];
             [hud hide:YES];
            [self.tableVIew1 reloadData];
            self.indePage = [[dic valueForKey:@"totalPage"] intValue];
            [self.tableVIew1.pullToRefreshView stopAnimating];
            [self.tableVIew1.infiniteScrollingView stopAnimating];

            
        }
        else{
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"网络加载失败";
            [hud hide:YES afterDelay:2];
            [self.tableVIew1.pullToRefreshView stopAnimating];
            [self.tableVIew1.infiniteScrollingView stopAnimating];

        }
    }];
    
}

#pragma mark---UITableViewDataSource,
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
    
    return self.datasDic.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QuanBuTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"quanbu" forIndexPath:indexPath];
    
    [cell.pingJiaBtn addTarget:self action:@selector(pingJiaBtn1:) forControlEvents:UIControlEventTouchUpInside];
    cell.pingJiaBtn.tag=indexPath.row;
    NSDictionary * dic1 = [self.datasDic objectAtIndex:indexPath.row];
    
    [cell.ImagView1 sd_setImageWithURL:[NSURL URLWithString:[dic1 valueForKey:@"headImg"]==[NSNull null]?@"":[dic1 valueForKey:@"headImg"]] placeholderImage:[UIImage imageNamed:@"like"]];
    cell.nameLable.text = [dic1 valueForKey:@"cusName"]==[NSNull null]?@"":[dic1 valueForKey:@"cusName"];
    cell.shiJianLabel.text = [dic1 valueForKey:@"timeType"]==[NSNull null]?@"":[dic1 valueForKey:@"timeType"];
    
    cell.riQiLabel.text = [self upDated:[dic1 valueForKey:@"startTime"]==[NSNull null]?0:[[dic1 valueForKey:@"startTime"] longValue]];
    
    cell.diZhiLabel.text =[dic1 valueForKey:@"cusAddress"]==[NSNull null]?@"":[dic1 valueForKey:@"cusAddress"];
    cell.jiaGeLabel.text =[NSString stringWithFormat:@"%@",[dic1 valueForKey:@"price"]==[NSNull null]?@"":[dic1 valueForKey:@"price"] ];

    return cell;
    
}
#pragma mark---,UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 170;
}

-(void)pingJiaBtn1:(UIButton *)sender{
    NSDictionary * dic1 = [self.datasDic objectAtIndex:sender.tag];

    PingJiaViewController * p =[[PingJiaViewController alloc]init];
    
    p.orderId = [dic1 valueForKey:@"id"] ;
    
    [self.navigationController pushViewController:p animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSString *)upDated:(long )str{
    
    if (str==0) {
        
        return @"";
        
    }
    
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:str/1000];
    
    NSDateFormatter * dataFormatter = [[NSDateFormatter alloc]init];
    
    [dataFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString * string= [dataFormatter stringFromDate:date];
    
    return string;
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
